import { defineStore } from 'pinia'
import { ref } from 'vue'
import { request } from '@/utils/request'
import type { WorkflowParam } from '@/types/workflow'

export const useAIStore = defineStore('ai', () => {
    const loading = ref(false)

    // 获取 AI 建议
    const getSuggestion = async (param: WorkflowParam, value: any) => {
        try {
            loading.value = true
            const response = await request.post('/ai/suggest', {
                param,
                value,
                type: param.type,
                context: {
                    nodeId: param.nodeId,
                    component: param.component
                }
            })
            return response.data.suggestion
        } finally {
            loading.value = false
        }
    }

    // 优化提示词
    const optimizePrompt = async (prompt: string) => {
        try {
            loading.value = true
            const response = await request.post('/ai/optimize-prompt', {
                prompt
            })
            return response.data.optimized
        } finally {
            loading.value = false
        }
    }

    // 生成工作流描述
    const generateDescription = async (workflow: any) => {
        try {
            loading.value = true
            const response = await request.post('/ai/generate-description', {
                workflow
            })
            return response.data.description
        } finally {
            loading.value = false
        }
    }

    return {
        loading,
        getSuggestion,
        optimizePrompt,
        generateDescription
    }
}) 